﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PimImporter
{
    internal partial class FrmLoad : Form
    {
        private delegate void SetBarCallback(int max, int value);
        private delegate void CloseFormCallback();
        private bool loaded = false;

        internal FrmLoad(string msg)
        {
            InitializeComponent();
            LblLoading.Text = msg;
        }

        private void FrmLoad_Load(object sender, EventArgs e)
        {
            loaded = true;
        }

        internal void SetBar(int max, int value)
        {
            if (PrbLoad != null)
            {
                if (PrbLoad.InvokeRequired)
                {
                    SetBarCallback call = new SetBarCallback(SetBar);
                    PrbLoad.Invoke(call, new object[] { max, value });
                }
                else
                {
                    if (PrbLoad.Maximum != max)
                    {
                        PrbLoad.Maximum = max;
                    }
                    PrbLoad.Value = value;
                }
            }
        }

        internal void CloseForm()
        {
            if (!this.IsDisposed)
            {
                if (this.InvokeRequired)
                {
                    CloseFormCallback call = new CloseFormCallback(CloseForm);
                    PrbLoad.Invoke(call, new object[] { });
                }
                else
                {
                    this.Close();
                }
            }
        }

        private void FrmLoad_Resize(object sender, EventArgs e)
        {
            if (loaded)
            {
                PrbLoad.Top = this.Height / 2 - PrbLoad.Height / 2;
                PrbLoad.Width = this.Width - Def.ResizeResolution(6, Convert.ToInt32(this.AutoScaleDimensions.Height));
                LblLoading.Top = PrbLoad.Top - PrbLoad.Left - LblLoading.Height;
            }
        }
    }
}